#include "appwindow.hpp"
#include <iostream>

AppWindow::AppWindow()
{
  set_title("488 Tetrominoes on the Wall");

  // A utility class for constructing things that go into menus, which
  // we'll set up next.
  using Gtk::Menu_Helpers::MenuElem;
  using Gtk::Menu_Helpers::RadioMenuElem;
  using Gtk::Menu_Helpers::CheckMenuElem;
  
    
  // Set up the application menu
  // The slot we use here just causes AppWindow::hide() on this,
  // which shuts down the application.
    m_menu_app.items().push_back(MenuElem("_Restart", Gtk::AccelKey("s"),
    sigc::mem_fun(m_viewer, &Viewer::restart)));
    m_menu_app.items().push_back(MenuElem("_Pause", Gtk::AccelKey("p"),
    sigc::mem_fun(m_viewer, &Viewer::pause)));
    m_menu_app.items().push_back(MenuElem("_Reset", Gtk::AccelKey("r"),
    sigc::mem_fun(m_viewer, &Viewer::reset)));
    m_menu_app.items().push_back(MenuElem("_Quit", Gtk::AccelKey("q"),
    sigc::mem_fun(*this, &AppWindow::hide)));

    // game menu
    m_menu_game.items().push_back(CheckMenuElem(
        "_Beveled", Gtk::AccelKey("a"), sigc::mem_fun(m_viewer, &Viewer::set_bevel)));
    m_menu_game.items().push_back(CheckMenuElem(
        "_Lighting", Gtk::AccelKey("l"), sigc::mem_fun(m_viewer, &Viewer::set_light)));
   // static_cast<Gtk::CheckMenuItem*>(&m_menu_game.items()[0])->set_active();
    //static_cast<Gtk::CheckMenuItem*>(&m_menu_game.items()[1])->set_active();
    
    // buffer menu
    m_menu_buffer.items().push_back(CheckMenuElem(
        "_Double_Buffer", Gtk::AccelKey("b"), 
        sigc::mem_fun(m_viewer, &Viewer::set_double_buffer)));
    static_cast<Gtk::CheckMenuItem*>(&m_menu_buffer.items()[0])->set_active();
    
    // mode slot
    sigc::slot1<void, Viewer::myDrawMode> mode_slot = 
  	sigc::mem_fun(m_viewer, &Viewer::set_draw_mode);
    
    // mode menu
    Gtk::RadioMenuItem::Group radio_group_mode;
	m_menu_drawmode.items().push_back( RadioMenuElem(
        radio_group_mode, "WireFrame", Gtk::AccelKey("w"), 
        sigc::bind(mode_slot, Viewer::WIREFRAME)));
	m_menu_drawmode.items().push_back( RadioMenuElem(
        radio_group_mode, "Face", Gtk::AccelKey("f"), 
        sigc::bind(mode_slot, Viewer::FACE)));
	m_menu_drawmode.items().push_back( RadioMenuElem(
        radio_group_mode, "MultiColoured", Gtk::AccelKey("m"), 
        sigc::bind(mode_slot, Viewer::MULTICOLOURED)));
    
    // speed slot
    sigc::slot1<void, Viewer::mySpeed> speed_slot = 
  	sigc::mem_fun(m_viewer, &Viewer::set_speed);
    // speed menu
    Gtk::RadioMenuItem::Group radio_group_speed;
	m_menu_speed.items().push_back( RadioMenuElem(
        radio_group_speed, "Slow", sigc::bind(speed_slot, Viewer::SLOW)));
	m_menu_speed.items().push_back( RadioMenuElem(
        radio_group_speed, "Medium", sigc::bind(speed_slot, Viewer::MEDIUM)));
	m_menu_speed.items().push_back( RadioMenuElem(
        radio_group_speed, "Fast", sigc::bind(speed_slot, Viewer::FAST)));
    
    
    m_menubar.items().push_back(MenuElem("_File", m_menu_app));
    m_menubar.items().push_back(MenuElem("_Draw Mode", m_menu_drawmode));
    m_menubar.items().push_back(MenuElem("_Speed", m_menu_speed));
    m_menubar.items().push_back(MenuElem("_Buffering", m_menu_buffer));
    m_menubar.items().push_back(MenuElem("_Game", m_menu_game));
  // Pack in our widgets
  
  // First add the vertical box as our single "top" widget
  add(m_vbox);

  // Put the menubar on the top, and make it as small as possible
  m_vbox.pack_start(m_menubar, Gtk::PACK_SHRINK);

  // Put the viewer below the menubar. pack_start "grows" the widget
  // by default, so it'll take up the rest of the window.
  m_viewer.set_size_request(300, 600);
  m_vbox.pack_start(m_viewer);

  show_all();
}

bool AppWindow::on_key_press_event( GdkEventKey *ev )
{
    if( ev->keyval == GDK_Left ) {
		m_viewer.moveLeft();
	} else if( ev->keyval == GDK_Right ) {
		m_viewer.moveRight();
	} else if( ev->keyval == GDK_Down ) {
		m_viewer.rotateCW();
	} else if( ev->keyval == GDK_Up ) {
		m_viewer.rotateCCW();
	} else if( ev->keyval == GDK_space ) {
		m_viewer.drop();
    } else if( ev->keyval == GDK_Shift_L || ev->keyval == GDK_Shift_R ) {
       m_viewer.set_scale_mode(true);
	} else {
		return Gtk::Window::on_key_press_event( ev );
	}
	return true;
}

bool AppWindow::on_key_release_event(GdkEventKey *ev)
{
    if( ev->keyval == GDK_Shift_L || ev->keyval == GDK_Shift_R ) {
        m_viewer.set_scale_mode(false);
    }
    return true;
}